﻿using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.ReadAPI.Applications.Commands.AccountCommand;
using B.S.BaseData.ReadAPI.Dtos;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BaseData.ReadAPI.Controllers
{
    /// <summary>
    /// 用户登录管理
    /// </summary>
    [Authorize]
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AccountController : ControllerBase
    {
        /// <summary>
        /// 中介者
        /// </summary>
        IMediator mediator;
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<AccountController> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator">中介者</param>
        /// <param name="logger">日志</param>
        public AccountController(IMediator mediator, ILogger<AccountController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="cmd">登录账号密码</param>
        /// <returns>返回用户实体</returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<ApiResult<LoginAccountRespDto>> LoginAccount([FromQuery] LoginCommand cmd)
        {
            try
            {
                var result = await mediator.Send(cmd);
                logger.LogInformation("用户" + cmd.LoginName + "在" + DateTime.Now + "成功登录该系统");
                return result;
            }
            catch (Exception ex)
            {
                logger.LogError("用户登录出错" + ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<ApiResult<List<Account>>> GetAccount()
        {
            try
            {
                GetAccountCommand request = new GetAccountCommand();
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                throw;
            }
        }






    }
}
